package io.renren.modules.store.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.annotation.NoRepeatSubmit;
import io.renren.common.exception.RRException;
import io.renren.modules.store.entity.TStoreCashSiteEntity;
import io.renren.modules.store.service.TStoreCashSiteService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.store.entity.TStoreEquipmentEntity;
import io.renren.modules.store.service.TStoreEquipmentService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author steakliu
 * @email 2319492349@qq.com
 * @date 2021-03-18 23:26:12
 */
@RestController
@RequestMapping("store/tstoreequipment")
public class TStoreEquipmentController extends AbstractController {
    @Autowired
    private TStoreEquipmentService tStoreEquipmentService;
    @Autowired
    private TStoreCashSiteService storeCashSiteService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("store:tstoreequipment:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("sellerId",getSeller());
        PageUtils page = tStoreEquipmentService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("store:tstoreequipment:info")
    public R info(@PathVariable("id") Long id){
		TStoreEquipmentEntity tStoreEquipment = tStoreEquipmentService.getById(id);

        return R.ok().put("tStoreEquipment", tStoreEquipment);
    }

    /**
     * 保存
     */
    @NoRepeatSubmit
    @RequestMapping("/save")
    //@RequiresPermissions("store:tstoreequipment:save")
    public R save(@RequestBody TStoreEquipmentEntity tStoreEquipment){
        tStoreEquipment.setSellerId(getSeller());
        tStoreEquipment.setCreateTime(new Date());
		tStoreEquipmentService.save(tStoreEquipment);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("store:tstoreequipment:update")
    public R update(@RequestBody TStoreEquipmentEntity tStoreEquipment){
		tStoreEquipmentService.updateById(tStoreEquipment);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("store:tstoreequipment:delete")
    public R delete(@RequestBody Long[] ids){
        TStoreCashSiteEntity one = storeCashSiteService.getOne(new QueryWrapper<TStoreCashSiteEntity>()
                .eq("seller_id", getSeller())
                .eq("equipment_id", ids[0])
                .eq("seller_id", getSeller())
        );
        if (one != null){
            throw new RRException("此设备下存在收银站点，不能删除");
        }
        tStoreEquipmentService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
